 <template>
 	<view class="login safe_area">
 		<image :src="getimageUrl('homedenglu.png')" class="login_img"></image>
 		<button class="btn_style" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信登录</button>
 	</view>
 </template>

 <script lang="ts" setup>
	import { getimageUrl } from '@/util/util.js';
	import { ref, onMounted, unref } from 'vue';
	import { wxPhoneLogin } from '@/common/sys.api';

	const loginCode = ref();

	onMounted(() => {
		uni.login({
			success: (res) => {
				console.log(res);
				loginCode.value = res.code;
			}
		})
	})

	function login() {
		uni.checkSession({
			success(res) {
				if (res?.errMsg?.indexOf('ok') < 0) {
					uni.login({
						success: (res) => {
							console.log(res);
							loginCode.value = res.code;
						}
					})
				}
				console.log(res, 'checkSession');
			}, fail(err) {
				console.log(err, 'err');
			}
		})
	}

	/**
	 * 登录
	 */
	async function getPhoneNumber(e) {
		console.log(e, 'e');
		const { encryptedData, iv, errMsg } = e.detail;
		if (errMsg?.indexOf('ok') >= 0) {
			uni.showLoading()
			let res = await wxPhoneLogin({ code: unref(loginCode), encryptedData, iv, loginType: '1' });
			uni.hideLoading();
			console.log(res, 'res');
			uni.$u.vuex('vuex_user', res.sysUser);
			uni.$u.vuex('vuex_token', res.token);
			uni.$u.vuex('vuex_openid', res.openid);
			// 第一次登录编辑资料
			if (res.isFirstLogin === '1') {
				uni.$u.route({
					type: 'reLaunch',
					url: '/pages/personalInformation/personalInformation',
					params: {
						type: 'login'
					}
				})
			} else {//不是第一次进入首页
				uni.$u.route({
					type: 'back',
				})
			}
		}
	}
 </script>

 <style lang="scss" scoped>
 	.login {
 		.login_img {
 			display: block;
 			width: 100vw;
 			height: 100vh;
 		}

 		.btn_style {
 			position: fixed;
 			bottom: 78rpx;
 			border-radius: 49rpx;
 			background-color: #FF5046;
 			font-size: 32rpx;
 			font-weight: 500;
 			height: 80rpx;
 			line-height: 80rpx;
 			width: 80vw;
 			margin: 0 calc(10vw) 0 calc(10vw);
 			color: #fff;
 		}
 	}
 </style>